﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using YXChatFormClient.Dtos.Buddy;
using YXChatFormClient.Global;
using YXChatFormClient.Utility;

namespace YXChatFormClient.UserControls
{
    public partial class UC_BuddysContainer : UserControl
    {
        private readonly HttpHelper _http;
        public UC_BuddysContainer()
        {
            _http = new HttpHelper();
            InitializeComponent();
        }
        private void UC_BuddysContainer_Paint(object sender, PaintEventArgs e)
        {
            PaintHelper.PaintBorder(e, this.ClientRectangle, Color.White,
                Color.White, Color.FromArgb(214, 214, 214), Color.White);
        }

        private void UC_BuddysContainer_Load(object sender, EventArgs e)
        {
            InitBuddyItemData();
        }
        private async void InitBuddyItemData()
        {
            var buddyAccounts=await _http.GetAsync<List<BuddyAccountOutput>>($"{Api.GetBuddys}/{GlobalAccountInfo.Account.AccountNumber}");
            foreach (var item in buddyAccounts)
            {
                UC_BuddyItem buddyItem = new UC_BuddyItem()
                {
                    Dock = DockStyle.Top,
                    BuddyAccountOutput = item
                };
                buddyItem.BuddyClick += BuddyItem_BuddyClick;
                
                this.panelContent.Controls.Add(buddyItem);
            }
        }

        public event UC_BuddyItem.BuddyClickHandler BuddyClick;
        private void BuddyItem_BuddyClick(BuddyAccountOutput buddyAccountOutput)
        {
            if (BuddyClick != null) BuddyClick(buddyAccountOutput);
        }

        public delegate void NewBuddyClickHandler(object sender, EventArgs e);
        public event NewBuddyClickHandler UserControlNewBuddyClicked;

        private void panelNewBuddy_MouseClick(object sender, MouseEventArgs e)
        {
            if (UserControlNewBuddyClicked != null) UserControlNewBuddyClicked(sender, e);
        }

        private void panelContent_Paint(object sender, PaintEventArgs e)
        {
            PaintHelper.PaintBorder(e, panelContent.ClientRectangle, Color.White,
                Color.White, Color.FromArgb(214, 214, 214), Color.White);
        }
    }
}
